package com.wenbei.service;

import com.wenbei.domain.User;
import com.wenbei.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
/**
 * 用户业务层
 * @author 北哥 公众号：BiggerBoy
 * @date 2022-9-8 17:39:31
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    public void updateById() {
        User record = new User();
        record.setId(9);
        record.setAge(1);
        userMapper.updateByPrimaryKeySelective(record);
        System.out.println("事务1 执行第一条SQL完毕");

        User record1 = new User();
        record1.setId(10);
        record1.setAge(2);
        userMapper.updateByPrimaryKeySelective(record1);
        System.out.println("事务1 执行第二条SQL完毕");
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateById1() {
        User record1 = new User();
        record1.setId(10);
        record1.setAge(3);
        userMapper.updateByPrimaryKeySelective(record1);
        System.out.println("事务2 执行第一条SQL完毕");


        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        User record = new User();
        record.setId(9);
        record.setAge(4);
        userMapper.updateByPrimaryKeySelective(record);
        System.out.println("事务2 执行第二条SQL完毕");
    }
}
